package com.nut.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.resource.CssLinkResourceTransformer;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.nut.pojo.CartSku;
import com.nut.pojo.ProductWithBLOBs;
import com.nut.pojo.Shopcart;
import com.nut.pojo.Sku;
import com.nut.service.color.ColorServiceI;
import com.nut.service.product.ProductServiceI;
import com.nut.service.shopcart.ShopcartServiceI;
import com.nut.service.sku.SkuServiceI;
import com.nut.service.storage.StorageServiceI;

/**
*@author Nut
*@version 2018年8月17日 上午10:07:19
*@TODO 用于商品的购物车和订单结算
*
*/
@Controller
@RequestMapping("pay")
public class PayController {
	
	@Resource
	SkuServiceI skuService;
	@Resource
	ShopcartServiceI shopcartService;
	@Resource
	ProductServiceI productService;
	@Resource
	ColorServiceI colorService;
	@Resource
	StorageServiceI storageService;

	@RequestMapping("addShopCart.do")
	@ResponseBody
	public String addShopCart(String id,String num){
		Shopcart shopcart = new Shopcart();
		shopcart.setSkuId(Integer.valueOf(id));
		shopcart.setUserId(1);
		shopcart.setNumber(Integer.valueOf(num));
		shopcartService.add(shopcart);
		System.out.println("id"+id+" num:"+num);
		return "shopcart";
	}
	
	@RequestMapping("goShopCart.do")
	public String goShopCart(int userId,ModelMap map){
		
		System.out.println("userId:"+userId);
		map.addAttribute("userId", userId);
		return "shopcart";
	}
	
	@RequestMapping("getSkus.do")
	@ResponseBody
	public List<CartSku> getSkus(int userId){
		List<CartSku> list = new ArrayList<>();//用于存放最终显示结果
		//System.out.println("@@"+userId);
		//读取给用户购物车中商品信息
		List<Shopcart> cart = new ArrayList<>();
		cart = shopcartService.querySkusByUserId(userId);
		
		//sku信息
		List<Sku> skus = new ArrayList<>();
		for (Shopcart shopcart : cart) {
			CartSku cs = new CartSku();
			cs.setNum(shopcart.getNumber());//-----------------------------数量
			cs.setId(shopcart.getId());//----------------------------------id
			//int skuId = shopcart.getSkuId();//sku  id
		
			Sku sku = skuService.queryOneBuId(shopcart.getSkuId());
			cs.setOriginalPrice(sku.getOriginalPrice());//-----------------原价
			cs.setPrice(sku.getPrice());//------------------------------现价
			cs.setColorName(colorService.queryOne(sku.getColorId()).getName());//-----------颜色名称
			cs.setStorageName(storageService.queryOne(sku.getStorageId()).getName());//------内存名称
			
		
			ProductWithBLOBs product = productService.queryOne(sku.getProductId());
			cs.setName(product.getName());//-----------------------------名字
			cs.setImage((product.getImages().split(";"))[0]);//--------------------图片

			list.add(cs);
		}
		return list;
	}
	
	@RequestMapping("gopay.do")
	@ResponseBody
	public String goPay(String cartskus,String id,String money,HttpServletRequest request){
		System.out.println("%%%%% "+id+":"+money);
		HttpSession session = request.getSession();
		session.setAttribute("id", id);
		session.setAttribute("money", money);
		
	    List<CartSku> list = (List<CartSku>) JSONArray.parseArray(cartskus,CartSku.class);
		for (CartSku cs : list) {
			//System.out.println(cs.getName());
			Shopcart sc = shopcartService.queryOne(cs.getId());
			sc.setNumber(cs.getNum());//得到要修改的数量
			shopcartService.updateOne(sc);//更新购物车中信息
		}
		return "pay";
	}
	
	@RequestMapping("goopay.do")
	public String goopay(HttpServletRequest request,ModelMap map){
		HttpSession session = request.getSession();
		String money = (String) session.getAttribute("money");
		String id = (String) session.getAttribute("id");
		List<Integer> list = (List<Integer>) JSONArray.parseArray(id,Integer.class);
		
		List<CartSku> cartSkus = new ArrayList<>();//用于显示最终订单结果
		
		List<Integer> csid = new ArrayList<>();//要删除的购物车中的id
		
		for (Integer i : list) {
			System.out.println(i); //购物车id
			CartSku cs = new CartSku();

			Shopcart shopcart = shopcartService.queryOne(i);
			cs.setNum(shopcart.getNumber());//--------------------------数量
			cs.setId(shopcart.getId());//----------------------------------id
			csid.add(shopcart.getId());
			
			Sku sku = skuService.queryOneBuId(shopcart.getSkuId());
			cs.setOriginalPrice(sku.getOriginalPrice());//-----------------原价
			cs.setPrice(sku.getPrice());//------------------------------现价
			cs.setColorName(colorService.queryOne(sku.getColorId()).getName());//-----------颜色名称
			cs.setStorageName(storageService.queryOne(sku.getStorageId()).getName());//------内存名称
			
		
			ProductWithBLOBs product = productService.queryOne(sku.getProductId());
			cs.setName(product.getName());//-----------------------------名字
			cs.setImage((product.getImages().split(";"))[0]);//--------------------图片

			cartSkus.add(cs);
		}
		
		session.setAttribute("csid", csid);
		System.out.println("*******"+cartSkus);
		map.addAttribute("cartSkus", cartSkus);
		map.addAttribute("money", money);
		
		return "pay";
	}
	
	@RequestMapping("success.do")
	@ResponseBody
	public String success(String address,String name,String tell,HttpServletRequest request){
		System.out.println(address+":"+name+":"+tell);
		HttpSession session = request.getSession();
		session.setAttribute("address", address);
		session.setAttribute("name", name);
		session.setAttribute("tell", tell);
		
		List<Integer> csid = (List<Integer>) session.getAttribute("csid");
		for (Integer integer : csid) {
			shopcartService.delete(integer);
		}

		return "pay";
	}
	
	@RequestMapping("goSuccess.do")
	public String goSuccess(HttpServletRequest request,ModelMap map){
		HttpSession session = request.getSession();
		String money = (String) session.getAttribute("money");
		String name = (String) session.getAttribute("name");
		String address = (String) session.getAttribute("address");
		String tell = (String) session.getAttribute("tell");
		map.addAttribute("money", money);
		map.addAttribute("address", address);
		map.addAttribute("tell", tell);
		map.addAttribute("name", name);
		
		return "success";
	}
	
	@RequestMapping("delsku.do")
	@ResponseBody
	public String delsku(String cartshop){
		 System.out.println("$$$$ "+cartshop);
		 shopcartService.delete(Integer.valueOf(cartshop));
		return "del";
	}
	
	
}
